<html><head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="/layuiAdmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiAdmin/css/admin.css" media="all">
<body layadmin-themealias="default">

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">添加角色</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" >
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="name" lay-verify="required" autocomplete="off" placeholder="请输入名称" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">权限</label>
                    @if ($auths)
                        @foreach ($auths as $auth)
                            <div>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="auth_id[]" title="{{$auth['name']}}" value="{{$auth['id']}}" lay-filter="lockDemo"  class="all auth_id"><div class="layui-unselect layui-form-checkbox"><span>{{$auth['name']}}</span><i class="layui-icon layui-icon-ok"></i></div>
                                </div>
                                <div class="layui-input-block">
                                    @foreach ($auth['child'] as $item)
                                        <input type="checkbox" name="auth_id[]" class="auth_id" title="{{$item['name']}}" value="{{$item['id']}}"  lay-filter="lockDemo1"><div class="layui-unselect layui-form-checkbox"><span>{{$item['name']}}</span><i class="layui-icon layui-icon-ok"></i></div>
                                    @endforeach
                                </div>
                            </div>
                        @endforeach
                    @else
                        <div class="layui-input-block">
                            <input type="text" name="" lay-verify="" autocomplete="off" placeholder="请先去设置权限" class="layui-input" disabled>
                        </div>
                    @endif


                </div>

                {{--      <div class="layui-card layui-form" lay-filter="component-form-element">
                          <div class="layui-card-header">权限</div>
                          <div class="layui-card-body layui-row layui-col-space10">
                              <div class="layui-col-md12">
                                  <input type="checkbox" name="" title="写作" ><div class="layui-unselect layui-form-checkbox layui-form-checked"><span>写作</span><i class="layui-icon layui-icon-ok"></i></div>
                                  <input type="checkbox" name="" title="发呆"><div class="layui-unselect layui-form-checkbox"><span>发呆</span><i class="layui-icon layui-icon-ok"></i></div>
                                  <input type="checkbox" name="" title="禁用" disabled=""><div class="layui-unselect layui-form-checkbox layui-checkbox-disbaled layui-disabled"><span>禁用</span><i class="layui-icon layui-icon-ok"></i></div>
                              </div>
                          </div>
                      </div>--}}



                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" id="remark" name="remark" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                        <button type="button" class="layui-btn layui-btn-primary" onclick="location.reload()">重置</button>
                        <button type="button" class="layui-btn layui-btn-primary" onclick="location.href='/admin/role'">返回列表</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="/layuiAdmin/layui/layui.js"></script>
<script src="/layuiAdmin/tool.js"></script>

<script>
    layui.use(['form'], function() {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.jquery;

        form.on('checkbox(lockDemo)', function(obj){
            let inputObj = $(this).parent().next().children('input');
            if(obj.elem.checked){
                inputObj.next().addClass('layui-form-checked');
            }else {
                inputObj.next().removeClass('layui-form-checked');
            };

        });

        form.on('checkbox(lockDemo1)', function(obj){
            let inputObj = $(this).parent().prev().children(".all");
            if(obj.elem.checked){
                inputObj.next().addClass('layui-form-checked');
            }else {
                if($(this).siblings('.layui-form-checked').length > 0){
                    inputObj.next().addClass('layui-form-checked');
                }else {
                    inputObj.next().removeClass('layui-form-checked');
                }

            };

        });

        form.on('submit(demo1)', function(data) {

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            var data = [];
            data['name'] = $('#name').val();
            data['auth_id'] = [];
            $(".layui-form-checked").prev().each(function () {
                data['auth_id'].push($(this).val());
            });
            data['remark'] = $('#remark').val();

            $.post('/admin/role/create' , {
                name : data['name'],
                auth_id : data['auth_id'],
                remark : data['remark']
            } , function (rJson) {

                if(rJson.status){
                    successAlert(rJson.msg);
                }else{
                    layer.msg(rJson.msg, {time: 5000, icon:5});
                }
            });
            return false;
        });


    });
</script>


</body>
</html>